package org.github.p2w;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.github.p2w.entity.Config;
import org.github.p2w.entity.Postman;
import org.github.p2w.util.Postman2Word;

import java.io.File;
import java.nio.charset.StandardCharsets;

/**
 * 主程序
 *
 * @author xiayongchao
 * @date 2022/08/02
 */
@Slf4j
public class App {

    public static void main(String[] args) {
        Config config = Config.getInstance();
        if (StrUtil.isBlank(config.getCollectionFilePath())) {
            throw new RuntimeException("Collections json文件路径不能为空");
        }
        if (StrUtil.isBlank(config.getWordSavePath())) {
            throw new RuntimeException("word文件保存路径不能为空");
        }
        // 读取集合json文件
        JSONObject jsonObject = JSONUtil.readJSONObject(new File(config.getCollectionFilePath()), StandardCharsets.UTF_8);
        Postman postman = JSONUtil.toBean(jsonObject, Postman.class);
        JSONObject environment = new JSONObject();
        if (StrUtil.isNotBlank(config.getEnvironmentFilePath())) {
            // 读取环境json文件
            environment = JSONUtil.readJSONObject(new File(config.getEnvironmentFilePath()), StandardCharsets.UTF_8);
        }
        String file = config.getWordSavePath() + postman.getFileName() + DateUtil.date().toString("yyyyMMddHHmmss") + ".docx";
        new Postman2Word(postman, environment).p2w().save(file);
        log.info("word文件路径：" + file);
    }

}
